CURRENT_DIR = `pwd`
INDEX_DIR = index
PAGE_RANK_DIR = pageRank
HTMLCXX_PATH = $(CURRENT_DIR)/$(INDEX_DIR)/htmlcxx-0.84
HTMLCXX_INCLUDE_PATH = $(HTMLCXX_PATH)/include
HTMLCXX_LIB_PATH = $(HTMLCXX_PATH)/lib

CXX = g++

CXXFLAGS = -O2 -Wno-deprecated -Wall
LIBSFLAGS = -lz -L $(HTMLCXX_LIB_PATH)
		
INCLUDES = -I $(INDEX_DIR)/riCode/src -I $(HTMLCXX_INCLUDE_PATH)

BINFOLDER = bin
TESTFOLDER = test

COMPILE = $(CXX) $(INCLUDES) -c $(CXXFLAGS)

QUERY_TARGET = $(BINFOLDER)/query
QUERY_OBJS = $(BINFOLDER)/Registry.o \
	$(BINFOLDER)/Run.o \
	$(BINFOLDER)/Lexon.o \
	$(BINFOLDER)/Parser.o \
	$(BINFOLDER)/Document.o \
	$(BINFOLDER)/CollectionReader.o \
	$(BINFOLDER)/IndexBuilder.o \
	$(BINFOLDER)/Index.o \
	$(BINFOLDER)/Encoder.o \
	$(BINFOLDER)/QueryResult.o \
	$(BINFOLDER)/TimeMeasurement.o \
	$(BINFOLDER)/DocumentInfo.o \
	$(BINFOLDER)/DocumentIndex.o \
	$(BINFOLDER)/AnchorText.o \
	$(BINFOLDER)/QueryMain.o
$(QUERY_TARGET):	$(QUERY_OBJS)
	$(CXX) $(QUERY_OBJS) $(HTMLCXX_LIB_PATH)/*.o $(LIBSFLAGS) $(INCLUDES) -o $(QUERY_TARGET)

INDEX_TARGET = $(BINFOLDER)/index 	
INDEX_OBJS = $(BINFOLDER)/Document.o \
	$(BINFOLDER)/CollectionReader.o \
	$(BINFOLDER)/Registry.o \
	$(BINFOLDER)/Run.o \
	$(BINFOLDER)/Lexon.o \
	$(BINFOLDER)/Parser.o \
	$(BINFOLDER)/IndexBuilder.o \
	$(BINFOLDER)/Encoder.o \
	$(BINFOLDER)/Index.o \
	$(BINFOLDER)/QueryResult.o \
	$(BINFOLDER)/TimeMeasurement.o \
	$(BINFOLDER)/DocumentInfo.o \
	$(BINFOLDER)/DocumentIndex.o \
	$(BINFOLDER)/AnchorText.o \
	$(BINFOLDER)/IndexMain.o
$(INDEX_TARGET):	$(INDEX_OBJS)
	$(CXX) $(INDEX_OBJS) $(HTMLCXX_LIB_PATH)/*.o $(LIBSFLAGS) $(INCLUDES) -o $(INDEX_TARGET)

PAGE_RANK_TARGET = $(BINFOLDER)/pageRank 	
PAGE_RANK_OBJS = $(BINFOLDER)/PageRank.o \
	$(BINFOLDER)/TimeMeasurement.o \
	$(BINFOLDER)/PageRankMain.o
$(PAGE_RANK_TARGET):	$(PAGE_RANK_OBJS)
	$(CXX) $(PAGE_RANK_OBJS) -o $(PAGE_RANK_TARGET)

RANK_TARGET = $(BINFOLDER)/rank
RANK_OBJS = $(BINFOLDER)/Registry.o \
	$(BINFOLDER)/Run.o \
	$(BINFOLDER)/Lexon.o \
	$(BINFOLDER)/Parser.o \
	$(BINFOLDER)/Document.o \
	$(BINFOLDER)/CollectionReader.o \
	$(BINFOLDER)/IndexBuilder.o \
	$(BINFOLDER)/Index.o \
	$(BINFOLDER)/Encoder.o \
	$(BINFOLDER)/QueryResult.o \
	$(BINFOLDER)/TimeMeasurement.o \
	$(BINFOLDER)/DocumentInfo.o \
	$(BINFOLDER)/DocumentIndex.o \
	$(BINFOLDER)/RankBuilder.o \
	$(BINFOLDER)/Rank.o \
	$(BINFOLDER)/AnchorText.o \
	$(BINFOLDER)/DocumentFeatures.o \
	$(BINFOLDER)/RankMain.o
$(RANK_TARGET):	$(RANK_OBJS)
	$(CXX) $(RANK_OBJS) $(HTMLCXX_LIB_PATH)/*.o $(LIBSFLAGS) $(INCLUDES) -o $(RANK_TARGET)


# Collection I/O
$(BINFOLDER)/Document.o: $(INDEX_DIR)/riCode/src/Document.cpp $(INDEX_DIR)/riCode/src/Document.h
	${COMPILE} $(INDEX_DIR)/riCode/src/Document.cpp -o $(BINFOLDER)/Document.o
$(BINFOLDER)/CollectionReader.o: $(INDEX_DIR)/riCode/src/CollectionReader.cpp $(INDEX_DIR)/riCode/src/CollectionReader.h $(INDEX_DIR)/riCode/src/envDefault.h
	${COMPILE} $(INDEX_DIR)/riCode/src/CollectionReader.cpp -o $(BINFOLDER)/CollectionReader.o

# Objects
$(BINFOLDER)/Registry.o: $(INDEX_DIR)/Registry.h $(INDEX_DIR)/Registry.cpp
	${COMPILE} $(INDEX_DIR)/Registry.cpp -o $(BINFOLDER)/Registry.o
$(BINFOLDER)/Run.o: $(INDEX_DIR)/Run.h $(INDEX_DIR)/Run.cpp
	${COMPILE} $(INDEX_DIR)/Run.cpp -o $(BINFOLDER)/Run.o
$(BINFOLDER)/Lexon.o: $(INDEX_DIR)/Lexon.h $(INDEX_DIR)/Lexon.cpp
	${COMPILE} $(INDEX_DIR)/Lexon.cpp -o $(BINFOLDER)/Lexon.o
$(BINFOLDER)/Parser.o: $(INDEX_DIR)/Parser.h $(INDEX_DIR)/Parser.cpp
	${COMPILE} $(INDEX_DIR)/Parser.cpp -o $(BINFOLDER)/Parser.o
$(BINFOLDER)/IndexBuilder.o: $(INDEX_DIR)/IndexBuilder.h $(INDEX_DIR)/IndexBuilder.cpp
	${COMPILE} $(INDEX_DIR)/IndexBuilder.cpp -o $(BINFOLDER)/IndexBuilder.o
$(BINFOLDER)/Index.o: $(INDEX_DIR)/Index.h $(INDEX_DIR)/Index.cpp
	${COMPILE} $(INDEX_DIR)/Index.cpp -o $(BINFOLDER)/Index.o
$(BINFOLDER)/Encoder.o: $(INDEX_DIR)/Encoder.h $(INDEX_DIR)/Encoder.cpp
	${COMPILE} $(INDEX_DIR)/Encoder.cpp -o $(BINFOLDER)/Encoder.o
$(BINFOLDER)/QueryResult.o: $(INDEX_DIR)/QueryResult.h $(INDEX_DIR)/QueryResult.cpp
	${COMPILE} $(INDEX_DIR)/QueryResult.cpp -o $(BINFOLDER)/QueryResult.o
$(BINFOLDER)/TimeMeasurement.o: $(INDEX_DIR)/TimeMeasurement.h $(INDEX_DIR)/TimeMeasurement.cpp
	${COMPILE} $(INDEX_DIR)/TimeMeasurement.cpp -o $(BINFOLDER)/TimeMeasurement.o
$(BINFOLDER)/DocumentInfo.o: $(INDEX_DIR)/DocumentInfo.cpp
	${COMPILE} $(INDEX_DIR)/DocumentInfo.cpp -o $(BINFOLDER)/DocumentInfo.o
$(BINFOLDER)/DocumentIndex.o: $(INDEX_DIR)/DocumentIndex.cpp
	${COMPILE} $(INDEX_DIR)/DocumentIndex.cpp -o $(BINFOLDER)/DocumentIndex.o
$(BINFOLDER)/QueryMain.o: $(INDEX_DIR)/QueryMain.cpp
	${COMPILE} $(INDEX_DIR)/QueryMain.cpp -o $(BINFOLDER)/QueryMain.o
$(BINFOLDER)/IndexMain.o: $(INDEX_DIR)/IndexMain.cpp
	${COMPILE} $(INDEX_DIR)/IndexMain.cpp -o $(BINFOLDER)/IndexMain.o
$(BINFOLDER)/AnchorText.o: $(INDEX_DIR)/AnchorText.cpp $(INDEX_DIR)/AnchorText.h
	${COMPILE} $(INDEX_DIR)/AnchorText.cpp -o $(BINFOLDER)/AnchorText.o

$(BINFOLDER)/PageRank.o: $(PAGE_RANK_DIR)/PageRank.cpp $(PAGE_RANK_DIR)/PageRank.h
	${COMPILE} $(PAGE_RANK_DIR)/PageRank.cpp -o $(BINFOLDER)/PageRank.o
$(BINFOLDER)/PageRankMain.o: $(PAGE_RANK_DIR)/PageRankMain.cpp
	${COMPILE} $(PAGE_RANK_DIR)/PageRankMain.cpp -o $(BINFOLDER)/PageRankMain.o

$(BINFOLDER)/DocumentFeatures.o: DocumentFeatures.cpp DocumentFeatures.h
	${COMPILE} DocumentFeatures.cpp -o $(BINFOLDER)/DocumentFeatures.o
$(BINFOLDER)/Rank.o: Rank.cpp Rank.h
	${COMPILE} Rank.cpp -o $(BINFOLDER)/Rank.o
$(BINFOLDER)/RankBuilder.o: RankBuilder.cpp RankBuilder.h
	${COMPILE} RankBuilder.cpp -o $(BINFOLDER)/RankBuilder.o
$(BINFOLDER)/RankMain.o: RankMain.cpp
	${COMPILE} RankMain.cpp -o $(BINFOLDER)/RankMain.o

# htmlcxx
htmlcxx:
	cd $(HTMLCXX_PATH) && \
		./configure && \
		make clean && \
		make
	cd ..
	@mkdir -pv $(HTMLCXX_INCLUDE_PATH)/htmlcxx/html
	cp $(HTMLCXX_PATH)/html/*.h $(HTMLCXX_INCLUDE_PATH)/htmlcxx/html/
	cp $(HTMLCXX_PATH)/html/ParserSax.tcc $(HTMLCXX_INCLUDE_PATH)/htmlcxx/html/
	cp $(HTMLCXX_PATH)/html/.libs/*.o $(HTMLCXX_LIB_PATH)
	cd $(HTMLCXX_PATH) && \
		make distclean
htmlcxx_clean:
	rm -rfv $(HTMLCXX_INCLUDE_PATH)/*
	rm -rfv $(HTMLCXX_LIB_PATH)/libhtmlcxx*.o

# Query
query:	$(QUERY_TARGET)
	
# Index
index:	$(INDEX_TARGET)

# PageRank
pageRank:	$(PAGE_RANK_TARGET)

# Rank
rank:	$(RANK_TARGET)

# all
all:	$(INDEX_TARGET) $(PAGE_RANK_TARGET) $(RANK_TARGET)

# run
run:	$(INDEX_TARGET) $(PAGE_RANK_TARGET) $(RANK_TARGET)
	./$(INDEX_TARGET) -i indexToCompressedColection.txt -f test/input/toyExample -t test/output/ \
		-a test/output/index -v test/output/vocabulary -d test/output/documents -p test/output/links \
		-l test/output/anchorTexts
	./$(PAGE_RANK_TARGET) -i test/output/links -p test/output/pageRanks
	./$(RANK_TARGET) -a test/output/index -v test/output/vocabulary -d test/output/documents \
		-p test/output/pageRanks -l test/output/anchorTexts

# clean
clean:
	rm -f $(BINFOLDER)/*
	rm -f $(TESTFOLDER)/output/*
